package reduce.fz.com.reduce.adapter;

import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.bean.Card;
import reduce.fz.com.reduce.bean.ReplyComment;
import reduce.fz.com.reduce.custom.CardOperBtn;
import reduce.fz.com.reduce.custom.HeaderFooterStatusRecyclerViewAdapter;
import reduce.fz.com.reduce.util.PicassoUtil;

/**
 * Created by qingshengzheng on 15/7/23.
 */
public class CardDetailAdapter extends HeaderFooterStatusRecyclerViewAdapter<AbstractRecycleHolder> {
    private Card card;
    private List<ReplyComment> replys;
    private Context context;
    private LayoutInflater layoutInflater = null;
    private CardDetailListener cardDetailListener;


    public CardDetailAdapter(Context context, Card card, List<ReplyComment> replys, CardDetailListener cardDetailListener) {
        this.card = card;
        this.context = context;
        this.replys = replys;
        this.cardDetailListener = cardDetailListener;
        this.layoutInflater = LayoutInflater.from(context);
    }


    @Override
    protected int getHeaderItemCount() {
        return 1;
    }

    @Override
    protected int getContentItemCount() {
        return replys.size();
    }

    public  static interface CardDetailListener {

        public void onPressNickName(Card card);

        public void onPressUserHead(Card card);

        public void onPressZan(Card card);

        public void onPressPinglun(Card card);

        public void onPressFenxiang(Card card);

        public void onPressGuanzhu(Card card);
    }


    private static class CardDetailHeaderHolder extends AbstractRecycleHolder<Card> implements View.OnClickListener {
        private TextView tv_user_name;
        private TextView pub_time;
        private ImageView iv_user_face;
        private TextView card_content;
        private CardOperBtn btn_zan;
        private CardOperBtn btn_pinglun;
        private CardOperBtn btn_fenxiang;
        private CardOperBtn btn_guanzhu;
        private CardDetailListener cardDetailListener;
        private Card card;


        public CardDetailHeaderHolder(View itemView, RecyclerView.Adapter adapter, CardDetailListener cardDetailListener) {
            super(itemView, adapter);
            this.cardDetailListener = cardDetailListener;
            tv_user_name = (TextView) itemView.findViewById(R.id.tv_user_name);
            pub_time = (TextView) itemView.findViewById(R.id.pub_time);
            iv_user_face = (ImageView) itemView.findViewById(R.id.iv_user_face);
            card_content = (TextView) itemView.findViewById(R.id.card_content);
            btn_zan = (CardOperBtn) itemView.findViewById(R.id.btn_zan);
            btn_pinglun = (CardOperBtn) itemView.findViewById(R.id.btn_pinglun);
            btn_fenxiang = (CardOperBtn) itemView.findViewById(R.id.btn_fenxiang);
            btn_guanzhu = (CardOperBtn) itemView.findViewById(R.id.btn_guanzhu);

            tv_user_name.setOnClickListener(this);
            pub_time.setOnClickListener(this);
            iv_user_face.setOnClickListener(this);
            btn_pinglun.setOnClickListener(this);
            btn_zan.setOnClickListener(this);
            btn_pinglun.setOnClickListener(this);
            btn_fenxiang.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.tv_user_name:
                    cardDetailListener.onPressNickName(card);
                    break;
                case R.id.iv_user_face:
                    cardDetailListener.onPressNickName(card);
                    break;
                case R.id.btn_zan:
                    cardDetailListener.onPressZan(card);
                    break;
                case R.id.btn_pinglun:
                    cardDetailListener.onPressPinglun(card);
                    break;
                case R.id.btn_fenxiang:
                    cardDetailListener.onPressFenxiang(card);
                    break;
                default:
                    break;
            }

        }

        @Override
        public void renderView(Card bean) {
            this.card = bean;
            tv_user_name.setText(bean.getUser().getMobile());
            pub_time.setText(bean.getPubdate().toString());
           // PicassoUtil.laodUrl(itemView.getContext(), bean.getUser().getHeadurl(), iv_user_face);
            card_content.setText(bean.getDesc());

        }

        @Override
        public void initView() {

        }

    }

    private static class CardDetailReplyrHolder extends AbstractRecycleHolder<ReplyComment> {
        private TextView username;
        private TextView tv_reply_content;
        private TextView tv_reply_date;
        private ImageView user_header;


        public CardDetailReplyrHolder(View itemView, RecyclerView.Adapter adapter) {
            super(itemView, adapter);
            username = (TextView) itemView.findViewById(R.id.tv_user_nickname);
            tv_reply_content = (TextView) itemView.findViewById(R.id.tv_reply_content);
            tv_reply_date = (TextView) itemView.findViewById(R.id.tv_reply_date);
            user_header = (ImageView) itemView.findViewById(R.id.user_header);

        }

        @Override
        public void renderView(ReplyComment bean) {
            username.setText(bean.getUser().getMobile());
            tv_reply_content.setText(bean.getDesc());
            tv_reply_date.setText(bean.getReplydate().toString());
           // PicassoUtil.laodUrl(itemView.getContext(), bean.getUser().getHeadurl(), user_header);
        }

        @Override
        public void initView() {

        }
    }

    @Override
    protected AbstractRecycleHolder onCreateHeaderItemViewHolder(ViewGroup parent, int headerViewType) {
        View view = layoutInflater.inflate(R.layout.view_card_detail_head, parent, false);
        CardDetailHeaderHolder viewHolder = new CardDetailHeaderHolder(view, this, cardDetailListener);
        return viewHolder;
    }

    @Override
    protected AbstractRecycleHolder onCreateContentItemViewHolder(ViewGroup parent, int contentViewType) {
        View view = layoutInflater.inflate(R.layout.layout_cardreply_item, parent, false);
        CardDetailReplyrHolder viewHolder = new CardDetailReplyrHolder(view, this);
        return viewHolder;
    }

    @Override
    protected void onBindHeaderItemViewHolder(AbstractRecycleHolder headerViewHolder, int position) {
        headerViewHolder.renderView(card);
    }

    @Override
    protected void onBindContentItemViewHolder(AbstractRecycleHolder contentViewHolder, int position) {
        contentViewHolder.renderView(this.replys.get(position));

    }


    @Override
    public AbstractRecycleHolder createFooterStatusViewHolder(View footerView) {
        return new AbstractRecycleHolder(footerView, this) {
            @Override
            public void renderView(Object bean) {

            }

            @Override
            public void initView() {

            }
        };
    }


}
